package com.tgky.ybzb.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tgky.ybzb.common.PageParam;
import com.tgky.ybzb.entity.FileInfo;
import com.tgky.ybzb.mapper.FileInfoMapper;
import com.tgky.ybzb.utils.CommonUtil;
import com.tgky.ybzb.utils.MPUtil;
import com.tgky.ybzb.entity.Jcmx;
import com.tgky.ybzb.mapper.JcmxMapper;
import com.tgky.ybzb.service.IJcmxService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tgky.ybzb.utils.TimeConvert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 决策模型表 服务实现类
 *
 * @author XYP
 * @since 2024-07-22
 */
@Service
public class JcmxServiceImpl extends ServiceImpl<JcmxMapper, Jcmx> implements IJcmxService {

    @Autowired
    private FileInfoMapper fileInfoMapper;
    @Autowired
    private JcmxMapper jcmxMapper;
    /**
     * 通过实体类获取QueryWrapper
     * @param pageParam 分页参数
     * @param jcmx 实体信息
     * @return
     */
    @Override
    public QueryWrapper<Jcmx> getQueryWrapper(PageParam pageParam, Jcmx jcmx){
        QueryWrapper<Jcmx> queryWrapper = MPUtil.getQueryWrapper(pageParam);
        queryWrapper.orderByDesc("create_time");
        if(jcmx == null){
            return queryWrapper;
        }

        //主键
        if(jcmx.getMxID() != null){
            queryWrapper.lambda()
                    .eq(Jcmx::getMxID, jcmx.getMxID());
            return queryWrapper;
        }

        //TODO 此处可以根据各字段查询需求修改查询条件，eq、like、ge、gt、le、lt、ne...等等等
        queryWrapper.lambda()
                .eq(jcmx.getMxmc() != null, Jcmx::getMxmc, jcmx.getMxmc())
                .eq(jcmx.getSfmc() != null, Jcmx::getSfmc, jcmx.getSfmc())
                .eq(jcmx.getAikj() != null, Jcmx::getAikj, jcmx.getAikj())
                .eq(jcmx.getKfyy() != null, Jcmx::getKfyy, jcmx.getKfyy())
                .eq(jcmx.getYxhj() != null, Jcmx::getYxhj, jcmx.getYxhj())
                .eq(jcmx.getMxlx() != null, Jcmx::getMxlx, jcmx.getMxlx())
                .eq(jcmx.getJcsd() != null, Jcmx::getJcsd, jcmx.getJcsd())
                .eq(jcmx.getJczql() != null, Jcmx::getJczql, jcmx.getJczql())
                .eq(jcmx.getBbxx() != null, Jcmx::getBbxx, jcmx.getBbxx());
        return queryWrapper;
    }

    /**
     * 保存上传的文件路径
     */
    @Transactional
    @Override
    public Boolean saveFilePathById(Jcmx jcmx) {
        int state=jcmxMapper.insert(jcmx);
        FileInfo fileInfo=jcmx.getFileInfo();
        fileInfo.setType("JCMX");
        fileInfo.setTableId(jcmx.getMxID());
        fileInfo.setCreateTime(TimeConvert.getCurrentDateTime());
        state=fileInfoMapper.insert(fileInfo);

        StringBuilder stringBuilder=new StringBuilder(fileInfo.getFilePath());
        int lastIndex=stringBuilder.lastIndexOf("/");
        String dictStr=stringBuilder.substring(0,lastIndex);

        //解压模型算法
        CommonUtil.unzipUsingCommonsCompress(fileInfo.getFilePath(),dictStr);
        return state==1 ? true:false;
    }

    /**
     * 更新决策模型
     * @param jcmx
     * @return
     */
    @Transactional
    @Override
    public boolean updateJcmxData(Jcmx jcmx) {
        int state=jcmxMapper.updateById(jcmx);
        state=fileInfoMapper.updateById(jcmx.getFileInfo());
        return state==1 ? true:false;
    }

    /**
     * 调用决策算法模型
     * @param jcmx
     * @return
     */
    @Override
    public String invokeJcmx(Jcmx jcmx) {
        return null;
    }


}
